package games.cowsAndBulls;

import java.util.Scanner;

/** Testing the game via console */

public class CowsAndBullsConsole {
	private static Scanner sc = new Scanner(System.in);

	public static void play() {
		System.out.print("Choose the number of digits [3 - 5]: ");
		int numberOfSymbols = readNumber();

		CowsAndBullsBusinessLogic game = new CowsAndBullsBusinessLogic(
				numberOfSymbols);
		
		if (numberOfSymbols < 3)
			System.out.println("The entered number is too small " +
					"and has been set to default minimum 3.");
		else if (numberOfSymbols > 5)
			System.out.println("The entered number is too large " +
					"and has been set to default maximum 5.");
		
		System.out.println("The system has generated a random number. "
				+ "The first digit cannot be 0. The digits cannot be repeated." 
				+ " Try to find it out.");

		while (!game.isWin()) {
			int number;
			do {
				System.out.print("> ");
				number = readNumber();
				game.getNumberFromClient(number);
			} while (!game.isNumberFromClientValid());
			System.out.println(number + " - " + game.getNumberOfCows()
					+ " cows, " + game.getNumberOfBulls() + " bulls.");
		}
	}

	private static int readNumber() {
		String input = sc.nextLine();
		int num = 0;
		try {
			num = Integer.parseInt(input);
		} catch (NumberFormatException e) {
			System.out.println("! " + input + " is not valid input! Try again. \n> ");
			readNumber();
		}

		return num;
	}
}
